﻿using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using BAL;
using Common;
using System.Net;

namespace Web
{
    public class UserValidate
    {
        /// <summary>
        /// 验证登陆
        /// </summary>
        /// <param name="req">传入Request对象</param>
        /// <param name="popedom">权限设定参数</param>
        /// <returns></returns>
        static public bool CheckLogin(HttpRequest req, string popedom)
        {
            bool t = false;
            UserLinkExtendInfo uinfo = new UserLinkExtendInfo();
            RoleInfo rinfo = new RoleInfo();
            if (req.Cookies["xxadmin"] != null)
            {
                uinfo = UserLinkExtend.GetSingleInfo(int.Parse(req.Cookies["xxadmin"]["UserId"]));
                if (uinfo.KeyOutDate > DateTime.Now)
                {
                    string Temp = DESEncrypt.Decrypt(req.Cookies["xxadmin"]["LoginKey"], uinfo.LoginKey.ToString());
                    if (Temp != "err")
                    {
                        string[] CookiesKeys = Regex.Split(Temp, ",,,");
                        if (uinfo.IdCard == CookiesKeys[0] && uinfo.PassWord == CookiesKeys[1])
                        {
                            if (uinfo.RoleId != 3)
                            {
                                if (popedom != "")
                                {
                                    rinfo = Role.GetRoleInfo(uinfo.RoleId);
                                    rinfo.RoleMain = "," + rinfo.RoleMain + ",";
                                    if (rinfo.RoleMain.IndexOf("," + popedom + ",") > -1)
                                    {
                                        t = true;
                                        if (Utility.GetTimerInterval(DateTime.Now, uinfo.KeyOutDate) < 30)
                                        {
                                            UserInfo User_Info = Users.GetUserInfoById(uinfo.Id);
                                            HttpCookie cookies = new HttpCookie("xxadmin");
                                            cookies.Values["UserId"] = uinfo.Id.ToString();
                                            string tCookiesKey = string.Join(",,,", new string[] { uinfo.IdCard, User_Info.PassWord });
                                            User_Info.LoginKey = Guid.NewGuid();
                                            User_Info.KeyOutDate = DateTime.Now.AddMinutes(180);
                                            cookies.Values["LoginKey"] = DESEncrypt.Encrypt(tCookiesKey, User_Info.LoginKey.ToString());
                                            cookies.Expires = DateTime.Now.AddMinutes(180);
                                            DelCookies();
                                            HttpContext.Current.Response.AppendCookie(cookies);
                                            Users.Edit(User_Info);
                                        }
                                    }
                                }
                                else
                                {
                                    t = true;
                                }
                            }
                        }
                    }
                    else
                    {
                        DelCookies();
                        
                    }
                }
                else
                {
                    DelCookies();
                }
            }
            return t;
        }

        static public UserLinkExtendInfo GetInfo(HttpRequest req)
        {
            UserLinkExtendInfo uinfo = new UserLinkExtendInfo();
            if (req.Cookies["xxadmin"] != null)
            {
                if (req.Cookies["xxadmin"]["UserId"] != "")
                {
                    uinfo = UserLinkExtend.GetSingleInfo(int.Parse(req.Cookies["xxadmin"]["UserId"]));
                }
            }
            return uinfo;
        }

        /// <summary>
        /// 删除Cookies
        /// </summary>
        public static void DelCookies()
        {
            if (HttpContext.Current.Request.Cookies["xxadmin"] != null)
            {
                HttpContext.Current.Response.Cookies.Remove("xxadmin");
                HttpContext.Current.Response.Cookies["xxadmin"].Expires = DateTime.Now.AddDays(-1);
            }
            else if (HttpContext.Current.Request.Cookies["Teacherxxx"] != null)
            {
                HttpContext.Current.Response.Cookies.Remove("Teacherxxx");
                HttpContext.Current.Response.Cookies["Teacherxxx"].Expires = DateTime.Now.AddDays(-1);
            }
        }

        /// <summary>
        /// 教师验证登陆
        /// </summary>
        /// <param name="req">传入Request对象</param>
        /// <returns></returns>
        static public bool TeacherCheckLogin(HttpRequest req)
        {
            bool t = false;
            UserLinkExtendInfo uinfo = new UserLinkExtendInfo();
            RoleInfo rinfo = new RoleInfo();
            ///修改
            ///
          if (req.Cookies["Teacherxxx"] != null)
          {
                uinfo = UserLinkExtend.GetSingleInfo(int.Parse(req.Cookies["Teacherxxx"]["UserId"]));
                string temp=DESEncrypt.Decrypt(req.Cookies["Teacherxxx"]["LoginKey"], uinfo.LoginKey.ToString()).ToString();
              if (temp!="err")
              {
                if (uinfo.KeyOutDate > DateTime.Now)
                {
                    string[] CookiesKeys = Regex.Split(temp, ",,,");
                    if (uinfo.IdCard == CookiesKeys[0] && uinfo.PassWord == CookiesKeys[1])
                    {
                        rinfo = Role.GetRoleInfo(uinfo.RoleId);
                        if (rinfo.Name.StartsWith("教师"))
                        {
                                
                                rinfo.RoleMain = "," + rinfo.RoleMain + ",";
                               
                                    t = true;
                                    if (Utility.GetTimerInterval(DateTime.Now, uinfo.KeyOutDate) < 30)
                                    {

                                        UserInfo User_Info = Users.GetUserInfoById(uinfo.Id);
                                        HttpCookie cookies = new HttpCookie("Teacherxxx");
                                        cookies.Values["UserId"] = uinfo.Id.ToString();
                                        string tCookiesKey = string.Join(",,,", new string[] { uinfo.IdCard, User_Info.PassWord });
                                        User_Info.LoginKey = Guid.NewGuid();
                                        User_Info.KeyOutDate = DateTime.Now.AddMinutes(180);
                                        cookies.Values["LoginKey"] = DESEncrypt.Encrypt(tCookiesKey, User_Info.LoginKey.ToString());
                                        cookies.Expires = DateTime.Now.AddMinutes(180);
                                        DelCookies();
                                        HttpContext.Current.Response.AppendCookie(cookies);
                                        Users.Edit(User_Info);
                                    }
                                    else
                                    {
                                        t = true;
                                    }
                        }else 
                        {
                             DelCookies();
                        }
                    }
                }
              }
              else
              {
                  DelCookies();
              }
            }
            return t;
            /// 
            ///
           // return t;
        }

        /// <summary>
        /// 获取用户的所有权限
        /// </summary>
        /// <returns></returns>
        public static string GetPopedom(HttpRequest req)
        {
            if (req.Cookies["xxadmin"] == null)
            {
                return "";
            }
            else
            {
                int userid = int.Parse(req.Cookies["xxadmin"]["UserId"]);
                UserInfo uinfo = Users.GetUserInfoById(userid);
                return Role.GetRoleByUserId(uinfo).RoleMain;
            }
        }

        /// <summary>
        /// 返回老师信息
        /// </summary>
        /// <param name="req">输入参数</param>
        /// <returns></returns>
        static public UserLinkExtendInfo TeacherGetInfo(HttpRequest req)
        {
            UserLinkExtendInfo uinfo = new UserLinkExtendInfo();
            if (req.Cookies["Teacherxxx"] != null)
            {
                if (req.Cookies["Teacherxxx"]["UserId"] != "")
                {
                    uinfo = UserLinkExtend.GetSingleInfo(int.Parse(req.Cookies["Teacherxxx"]["UserId"]));
                }
            }
            return uinfo;
        }

        public static bool CheckQuery()
        {
            if (HttpContext.Current.Session["QueryStudentId"] == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
